package com.tmri.clean;

import com.tmri.clean.init.CleanProperties;
import com.tmri.clean.init.OracleHandler;
import org.csource.fastdfs.*;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2021/10/19 10:55
 * @Created by xielx
 */
public class MainCleanClass {
    private static JdbcTemplate jdbcTemplate;
    private static StorageClient storageClient;

    public static void main(String[] args) throws Exception {
        init();
        String queryQueue = "select bm,rowid from TEMP_RIS_FDFS where rownum<=200";
        String queueDetail = "select bm,file_path from net_rural_dtgl_files where bm in %s";
        String deleteQueue = "delete from TEMP_RIS_FDFS where rowid=?";
        String deleteDetail= "delete from net_rural_dtgl_files where bm=?";
        List<Object[]> batchDelArgs = new ArrayList<>();
        do {
            batchDelArgs.clear();
            List<Map<String, Object>> queueList = jdbcTemplate.query(queryQueue, new ColumnMapRowMapper());
            if (null == queueList || queueList.size() == 0) {
                System.out.println("已经没有数据可删除");
                return;
            }
            StringBuilder where = new StringBuilder("(");
            for (Map<String, Object> map : queueList) {
                where.append("'").append(map.get("BM")).append("',");
                batchDelArgs.add(new Object[]{map.get("ROWID")});
            }
            where.replace(where.length() - 1, where.length(), ")");
            List<Map<String, Object>> detailList = jdbcTemplate.query(String.format(queueDetail, where.toString()), new ColumnMapRowMapper());
            jdbcTemplate.batchUpdate(deleteQueue,batchDelArgs);
            jdbcTemplate.execute("commit");
            batchDelArgs.clear();
            for (Map<String, Object> detail : detailList) {
                String path = detail.get("FILE_PATH").toString();
                String[] str = splitFdfsFileId(path);
                if (null ==str) {
                    continue;
                }
                System.out.print(String.format("开始删除BM：%s\t path:%s\t执行成功，返回：",detail.get("BM"),path));
                int result = storageClient.delete_file(str[0],str[1]);
                System.out.println(result);
                if (0 == result) {
                    batchDelArgs.add(new Object[]{detail.get("BM")});
                }
            }
            jdbcTemplate.batchUpdate(deleteDetail,batchDelArgs);
            jdbcTemplate.execute("commit");
        } while (true);
    }

    protected static String[] splitFdfsFileId(String remoteFileId) {
        if (null == remoteFileId || remoteFileId.length() == 0||remoteFileId.startsWith("http")) {
            return null;
        }
        if (remoteFileId.startsWith(StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR)) {
            remoteFileId = remoteFileId.substring(1);
        }
        String[] result = new String[2];
        int pos = remoteFileId.indexOf(
            StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR, 1);
        result[0] = remoteFileId.substring(0, pos);// 生成出来的groupname，没有/
        result[1] = remoteFileId.substring(pos + 1, remoteFileId.length());// 生成出来的fileid没有/
        return result;
    }

    private static void init() throws Exception {
        CleanProperties.init();
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getTrackerServer();
        StorageServer storageServer = null;
        storageClient = new StorageClient(trackerServer, storageServer);
        jdbcTemplate = OracleHandler.getJdbcTemplate();
    }
}
